<!DOCTYPE html>
<html>
    <head>
        <title>Test MediaKeySession not callable immediately after CreateSession().</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            // After creation, the MediaKeySession object is not
            // callable, and we should get a InvalidStateError.

            promise_test(function()
            {
                return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var mediaKeySession = mediaKeys.createSession();

                    var arbitraryResponse = new Uint8Array([0x00, 0x11]);
                    return mediaKeySession.update(arbitraryResponse).then(function(result) {
                        assert_unreached('update() succeeded unexpectedly.');
                    }).catch(function(error) {
                        assert_equals(error.name, 'InvalidStateError');
                    });
                });
            }, 'Update() immediately after CreateSession().');

            promise_test(function()
            {
                return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var mediaKeySession = mediaKeys.createSession();

                    return mediaKeySession.close().then(function(result) {
                        assert_unreached('close() succeeded unexpectedly.');
                    }).catch(function(error) {
                        assert_equals(error.name, 'InvalidStateError');
                    });
                });
            }, 'Close() immediately after CreateSession().');

            promise_test(function()
            {
                return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var mediaKeySession = mediaKeys.createSession();

                    return mediaKeySession.remove().then(function(result) {
                        assert_unreached('remove() succeeded unexpectedly.');
                    }).catch(function(error) {
                        assert_equals(error.name, 'InvalidStateError');
                    });
                });
            }, 'Remove() immediately after CreateSession().');
        </script>
    </body>
</html>

